'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0105
    Inherits Snac

    Public Sub New()
        MyBase.New(&H1, &H5)
    End Sub

    Private _ServiceFamily As New TlvServiceFamilyId
    Public ReadOnly Property ServiceFamily() As TlvServiceFamilyId
        Get
            Return _ServiceFamily
        End Get
    End Property

    Private _ServerAddress As New TlvServerAddress
    Public ReadOnly Property ServerAddress() As TlvServerAddress
        Get
            Return _ServerAddress
        End Get
    End Property

    Private _AuthorizationCookie As New TlvAuthorizationCookie
    Public ReadOnly Property AuthorizationCookie() As TlvAuthorizationCookie
        Get
            Return _AuthorizationCookie
        End Get
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return _ServiceFamily.CalculateTotalSize + _ServerAddress.CalculateTotalSize + _AuthorizationCookie.CalculateTotalSize
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        index += 8

        Do While index < data.Count
            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &HD
                    _ServiceFamily.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H5
                    _ServerAddress.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H6
                    _AuthorizationCookie.Deserialize(data.GetRange(index, desc.TotalSize))
            End Select

            index += desc.TotalSize
        Loop

        SetTotalSize(index)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(_ServiceFamily.Serialize)
        data.AddRange(_ServerAddress.Serialize)
        data.AddRange(_AuthorizationCookie.Serialize)

        Return data
    End Function
End Class

Public Class TlvServiceFamilyId
    Inherits Tlv

    Public Sub New()
        MyBase.New(&HD)
    End Sub

    Private _ServiceFamilyId As Integer
    Public Property ServiceFamilyId() As Integer
        Get
            Return _ServiceFamilyId
        End Get
        Set(ByVal value As Integer)
            _ServiceFamilyId = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ServiceFamilyId = ByteConverter.ToUInt16(data.GetRange(index, 2))
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_ServiceFamilyId)))

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function
End Class

Public Class TlvServerAddress
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H5)
    End Sub

    Private _ServerAddress As String
    Public Property ServerAddress() As String
        Get
            Return _ServerAddress
        End Get
        Set(ByVal value As String)
            _ServerAddress = value
        End Set
    End Property

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(_ServerAddress))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        _ServerAddress = ByteConverter.ToString(data.GetRange(4, DataSize))
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return _ServerAddress.Length
    End Function
End Class

'Public Class TlvScreenName
'    Inherits Tlv

'    Private _ScreenName As String
'    Public Property ScreenName() As String
'        Get
'            Return _ScreenName
'        End Get
'        Set(ByVal value As String)
'            _ScreenName = value
'        End Set
'    End Property

'    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
'        Dim data As List(Of Byte) =  MyBase.Serialize()

'        data.AddRange(ByteConverter.GetBytes(_ScreenName))

'        Return data
'    End Function

'    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
'        MyBase.Deserialize(data)

'        _ScreenName = ByteConverter.ToString(data.GetRange(4, DataSize))
'    End Sub

'    public Overrides Function CalculateDataSize() As Integer
'        Return _ScreenName.Length
'    End Function
'End Class

'Public Class TlvAuthorizationCookie
'    Inherits Tlv

'    Public Sub New()
'        MyBase.New(&H6)
'    End Sub

'    Private _AutorizationCode As String
'    Public Property AutorizationCode() As String
'        Get
'            Return _AutorizationCode
'        End Get
'        Set(ByVal value As String)
'            _AutorizationCode = value
'        End Set
'    End Property

'    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
'        Dim data As List(Of Byte) =  MyBase.Serialize()

'        data.AddRange(ByteConverter.GetBytes(_AutorizationCode))

'        Return data
'    End Function

'    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
'        MyBase.Deserialize(data)

'        _AutorizationCode = ByteConverter.ToString(data.GetRange(4, DataSize))
'    End Sub

'    Public Overrides Function CalculateDataSize() As Integer
'        Return _AutorizationCode.Length
'    End Function
'End Class